﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;

using Cisoft.Cisoft3G.BLL;
using Cisoft.Cisoft3G.Model;

public partial class Business_Cisoft3G_CountByDay : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            txtStartDate.Text = Cisoft.IntegrationSystem.Common.DateTimeHelper.GetThisWeekendFirst();
            txtEndDate.Text = Cisoft.IntegrationSystem.Common.DateTimeHelper.GetThisWeekendLast();

            ltScript.Text = "<script type=\"text/javascript\" src=\"../../js/My97DatePicker/WdatePicker.js\"></script>";
            //Chart1.Visible = false;

            btnQuery_Click(sender, e);
        }
    }

    #region 查询事件
    /// <summary>
    /// 查询事件
    /// </summary>
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        DataTable dt = GetDatatable();

        //每日
        Chart1.Titles.Add(txtStartDate.Text + "至" + txtEndDate.Text + "数据统计");
        Chart1.Titles[0].TextStyle = TextStyle.Shadow;
        Chart1.Titles[0].Font = new System.Drawing.Font("黑体", 14);

        //Chart1.ChartAreas["ChartArea1"].Area3DStyle.Enable3D = true;
        //曲线图
        #region 曲线图
        if (rbtnQxt.Checked)
        {
            foreach (DataRow row in dt.Rows)
            {
                bool b = false;
                string seriesName = row["dataname"].ToString();

                //判断是否显示
                foreach (Control c in this.form1.Controls)
                {
                    if (c is CheckBox)
                    {
                        CheckBox cb = (CheckBox)c;
                        if (cb.Text == seriesName && cb.Checked == true)
                        {
                            b = true;
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                if (b)
                {
                    Chart1.Series.Add(seriesName);
                }
                else
                {
                    continue;
                }
                //Chart1.Series.Add(seriesName);
                Chart1.Series[seriesName].ChartType = SeriesChartType.Spline;
                Chart1.Series[seriesName].BorderWidth = 3;
                Chart1.Series[seriesName].ShadowOffset = 2;

                for (int colIndex = 1; colIndex < dt.Columns.Count; colIndex++)
                {
                    string columnName = dt.Columns[colIndex].ColumnName;
                    string y = row[colIndex].ToString().Trim();
                    int YVal = int.Parse(y);

                    Chart1.Series[seriesName].Points.AddXY(columnName, YVal);
                }
                //显示数字
                Chart1.Series[seriesName].IsValueShownAsLabel = true;
            }
        }
        #endregion

        //柱状图
        #region 柱状图
        if (rbtnZzt.Checked)
        {
            foreach (DataRow row in dt.Rows)
            {
                bool b = false;
                string seriesName = row["dataname"].ToString();

                //判断是否显示
                foreach (Control c in this.form1.Controls)
                {
                    if (c is CheckBox)
                    {
                        CheckBox cb = (CheckBox)c;
                        if (cb.Text == seriesName && cb.Checked == true)
                        {
                            b = true;
                            break;
                        }
                        else
                        {
                            continue;
                        }
                    }
                }
                if (b)
                {
                    Chart1.Series.Add(seriesName);
                }
                else
                {
                    continue;
                }
                //Chart1.Series.Add(seriesName);
                Chart1.Series[seriesName].ChartType = SeriesChartType.Column;
                Chart1.Series[seriesName]["PointWidth"] = "0.6";

                for (int colIndex = 1; colIndex < dt.Columns.Count; colIndex++)
                {
                    // For each column (column 1 and onward) add the value as a point
                    string columnName = dt.Columns[colIndex].ColumnName;
                    string y = row[colIndex].ToString().Trim();
                    int YVal = int.Parse(y);

                    Chart1.Series[seriesName].Points.AddXY(columnName, YVal);
                }
                //显示数字
                Chart1.Series[seriesName].IsValueShownAsLabel = true;
            }
        }
        #endregion

        Chart1.Visible = true;
    }
    #endregion 

    #region 自定义DataTable DataTable GetDatatable()
    /// <summary>
    /// 自定义DataTabl
    /// </summary>
    private DataTable GetDatatable()
    {
        DataTable dt = new DataTable();

        //获取间隔天数
        int dayCount = Convert.ToInt32((Convert.ToDateTime(txtEndDate.Text) - Convert.ToDateTime(txtStartDate.Text)).TotalDays);
        //添加列
        dt.Columns.Add(new DataColumn("DataName"));
        for (int i = 0; i <= dayCount; i++)
        {
            dt.Columns.Add(new DataColumn(Convert.ToDateTime(txtStartDate.Text).AddDays(i).ToShortDateString()));
        }
        //dt.Columns.Add(new DataColumn(Convert.ToDateTime(txtEndDate.Text).AddDays(1).ToShortDateString()));
        //添加行
        #region 添加行

        dt.Rows.Add(GetInboxRow(dt));
        dt.Rows.Add(GetSentRow(dt));
        dt.Rows.Add(GetOutboxRow(dt));
        dt.Rows.Add(GetDraftRow(dt));

        List<List<int>> typeList = GetTypeList(dt);
        List<string> typeNameList = GetTypeNameList();

        for (int i = 1; i <= typeNameList.Count; i++)
        {
            DataRow drow = dt.NewRow();
            List<string> itemList = new List<string>();
            itemList.Add(typeNameList[i - 1]);
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                try
                {
                    itemList.Add(typeList[i - 1][j].ToString());
                }
                catch (Exception ex)
                {
                    continue;
                }
            }
            drow.ItemArray = itemList.ToArray();
            dt.Rows.Add(drow);
        }

        #endregion
        return dt;
    }
    #endregion

    #region 短信接收
    private DataRow GetInboxRow(DataTable dt)
    {
        DataRow row = dt.NewRow();
        List<string> dataList = new List<string>();
        dataList.Add("短信接收");

        for (int i = 1; i < dt.Columns.Count; i++)
        {
            int rowCount = new Cisoft.Cisoft3G.BLL.InboxBO().GetList(" IB_SendTime >= '" +
                dt.Columns[i].ColumnName + " 00:00:00' and IB_SendTime <= '" + dt.Columns[i].ColumnName + " 23:59:59' ").Tables[0].Rows.Count;
            dataList.Add(rowCount.ToString());
        }

        object[] obs = dataList.ToArray();

        row.ItemArray = obs;

        return row;
    }
    #endregion

    #region 发件箱
    private DataRow GetSentRow(DataTable dt)
    {
        DataRow row = dt.NewRow();
        List<string> dataList = new List<string>();
        dataList.Add("发件箱");

        for (int i = 1; i < dt.Columns.Count; i++)
        {
            int rowCount = new Cisoft.Cisoft3G.BLL.SentBO().GetList(" ST_SentTime >= '" +
                dt.Columns[i].ColumnName + " 00:00:00' and ST_SentTime <= '" + dt.Columns[i].ColumnName + " 23:59:59' ").Tables[0].Rows.Count;
            dataList.Add(rowCount.ToString());
        }

        object[] obs = dataList.ToArray();

        row.ItemArray = obs;

        return row;
    }
    #endregion

    #region 待发箱
    private DataRow GetOutboxRow(DataTable dt)
    {
        DataRow row = dt.NewRow();
        List<string> dataList = new List<string>();
        dataList.Add("待发箱");

        for (int i = 1; i < dt.Columns.Count; i++)
        {
            int rowCount = new Cisoft.Cisoft3G.BLL.OutboxBO().GetList(" OB_SendTime >= '" +
                dt.Columns[i].ColumnName + " 00:00:00' and OB_SendTime <= '" + dt.Columns[i].ColumnName + " 23:59:59' ").Tables[0].Rows.Count;
            dataList.Add(rowCount.ToString());
        }

        object[] obs = dataList.ToArray();

        row.ItemArray = obs;

        return row;
    }
    #endregion

    #region 草稿箱
    private DataRow GetDraftRow(DataTable dt)
    {
        DataRow row = dt.NewRow();
        List<string> dataList = new List<string>();
        dataList.Add("草稿箱");

        for (int i = 1; i < dt.Columns.Count; i++)
        {
            int rowCount = new Cisoft.Cisoft3G.BLL.DraftsBO().GetList(" DT_SendTime >= '" +
                dt.Columns[i].ColumnName + " 00:00:00' and DT_SendTime <= '" + dt.Columns[i].ColumnName + " 23:59:59' ").Tables[0].Rows.Count;
            dataList.Add(rowCount.ToString());
        }

        object[] obs = dataList.ToArray();

        row.ItemArray = obs;

        return row;
    }
    #endregion

    #region 获得短信类型数据列表 List<List<int>> GetTypeList(DataTable dt)
    /// <summary>
    /// 获得短信类型数据列表
    /// </summary>
    private List<List<int>> GetTypeList(DataTable dt)
    {
        List<List<int>> list = new List<List<int>>();

        for (int i = 1; i <= new Cisoft.Cisoft3G.BLL.SmsTypeBO().GetAllList().Tables[0].Rows.Count; i++)
        {
            List<int> dayList = new List<int>();
            for (int j = 1; j < dt.Columns.Count; j++)
            {
                int temp1 = new Cisoft.Cisoft3G.BLL.SentBO().GetList(" ST_SentTime >= '" +
                    dt.Columns[j].ColumnName + " 00:00:00' and ST_SentTime <= '" + dt.Columns[j].ColumnName +
                    " 23:59:59' and ST_SmsType like'%" + new Cisoft.Cisoft3G.BLL.SmsTypeBO().GetModel(i).ST_TypeName.Trim() + "%' ").Tables[0].Rows.Count;
                int temp2 = new Cisoft.Cisoft3G.BLL.OutboxBO().GetList(" OB_SendTime >= '" + dt.Columns[j].ColumnName + " 00:00:00' and OB_SendTime <= '" + dt.Columns[j].ColumnName + " 23:59:59' and RT_Type_FK =" + i.ToString()).Tables[0].Rows.Count;
                int temp3 = new Cisoft.Cisoft3G.BLL.InboxBO().GetList(" IB_SendTime >= '" + dt.Columns[j].ColumnName + " 00:00:00'  and IB_SendTime <= '" + dt.Columns[j].ColumnName + " 23:59:59' and RT_Type_FK =" + i.ToString()).Tables[0].Rows.Count;
                int c = temp1 + temp2 + temp3;
                dayList.Add(c);
            }
            list.Add(dayList);
        }

        return list;
    }
    #endregion

    #region 获得短信类型名称列表 List<string> GetTypeNameList()
    /// <summary>
    /// 获得短信类型名称列表
    /// </summary>
    private List<string> GetTypeNameList()
    {
        List<string> list = new List<string>();

        foreach (Cisoft.Cisoft3G.Model.SmsTypeMO model in new Cisoft.Cisoft3G.BLL.SmsTypeBO().GetModelList(""))
        {
            if (model != null)
            {
                list.Add(model.ST_TypeName.Trim());
            }
        }
        return list;
    }
    #endregion

}
